/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/

#include "zb32l03x_hal.h"
#include "bsp_usart.h"
#include "Ringbuff.h"
#include <stdio.h>
static FlagStatus TcFlag;/*发送完成标志*/
static unsigned char uartrxbuf[32];         /*接收缓冲区 */
static Ring_t rbuartrx;
/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
*   说    明：
*********************************************************************************************************
*/

void bsp_usart_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    UART_HandleTypeDef  UART_InitStruct = {0};

    BSP_USART_USART_CLKENABLE();
    BSP_USART_RX_CLKENABLE();
    BSP_USART_TX_CLKENABLE();

    RingBuff_Create(&rbuartrx, uartrxbuf, sizeof(uartrxbuf)); 

    /*TXD*/
    GPIO_InitStruct.Pin = BSP_USART_TX_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF;
    GPIO_InitStruct.OpenDrain = GPIO_PUSHPULL;
    GPIO_InitStruct.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStruct.SlewRate = GPIO_SLEW_RATE_HIGH;
    GPIO_InitStruct.DrvStrength = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Alternate = BSP_USART_TX_FUN;
    HAL_GPIO_Init(BSP_USART_TX_PORT, &GPIO_InitStruct);

    /*RXD*/
    GPIO_InitStruct.Pin = BSP_USART_RX_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF;
    GPIO_InitStruct.OpenDrain = GPIO_PUSHPULL;
    GPIO_InitStruct.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStruct.SlewRate = GPIO_SLEW_RATE_HIGH;
    GPIO_InitStruct.DrvStrength = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Alternate = BSP_USART_RX_FUN;
    HAL_GPIO_Init(BSP_USART_RX_PORT, &GPIO_InitStruct);


    /* UART module init.*/
    UART_InitStruct.Instance               = BSP_USART_USART_BASE;
    UART_InitStruct.Init.BaudRate          = BSP_USART_USART_BAUDRATE;
    UART_InitStruct.Init.BaudDouble        = UART_BAUDDOUBLE_DISABLE;
    UART_InitStruct.Init.WordLength        = UART_WORDLENGTH_8B;
    UART_InitStruct.Init.Parity            = UART_PARITY_NONE;
    UART_InitStruct.Init.Mode              = UART_MODE_TX_RX;
    HAL_UART_Init(&UART_InitStruct);
    TcFlag=RESET;
    __HAL_UART_ENABLE_IT(&UART_InitStruct, UART_IT_TC);
    __HAL_UART_ENABLE_IT(&UART_InitStruct, UART_IT_RXNE);
     /* Configure UART NVIC interrupt*/
    HAL_NVIC_SetPriority(BSP_USART_USART_IRQ, PRIORITY_LOWEST);
    NVIC_EnableIRQ(BSP_USART_USART_IRQ);
}

/*
*********************************************************************************************************
*	功能说明: 发送一个缓冲区
*	形    参: 
*	返 回 值: 
*   说    明：
*********************************************************************************************************
*/
void bsp_usart_sendbuff(uint8_t *buff, uint32_t len)
{
  uint32_t i;
  uint32_t tick;
  for (i = 0; i < len; i++)
  {
    TcFlag = RESET;
    BSP_USART_USART_BASE->SBUF = (buff[i] & (uint8_t)0xFF);
    
    while ((TcFlag == RESET));
    BSP_USART_USART_BASE->INTCLR |= UART_FLAG_TC;
  }
}

uint32_t bsp_usart_read(uint8_t *buff)
{
    uint32_t ret;
    ERR_E err ;
    ret=RingBuff_Read(&rbuartrx, (uint8_t *)buff, 32, &err);
    return ret;
}

/*
*********************************************************************************************************
*	功能说明: 底层接收一个缓冲区。
*	形    参: buff：缓冲区首地址
*           len：缓冲区长度
*	返 回 值: 无
*   说    明：
*********************************************************************************************************
*/
void UART1_IRQHandler(void)
{

    uint32_t isrflags   = READ_REG(BSP_USART_USART_BASE->INTSR);
    uint32_t sconits    = READ_REG(BSP_USART_USART_BASE->SCON);
    uint32_t errorflags = 0x00U;
    uint8_t  date[1];
    ERR_E err ;

    /* If no error occurs */
    errorflags = (isrflags & (uint32_t)UART_INTSR_FE);
    if(errorflags == RESET)
    {
        /* UART in mode Receiver -------------------------------------------------*/
        if(((isrflags & UART_INTSR_RI) != RESET) && ((sconits & UART_SCON_RIEN) != RESET))
        {
            BSP_USART_USART_BASE->INTCLR |= (UART_FLAG_RXNE);
            date[0]=(uint8_t)(BSP_USART_USART_BASE->SBUF & (uint8_t)0xFF);
            RingBuff_Write(&rbuartrx, (uint8_t *)date, 1, &err);
        }
    }

    /* If some errors occur */
    if (errorflags != RESET)
    {
      /* UART frame error occurred -----------------------------------*/
      if ((isrflags & UART_INTSR_FE) != RESET)
      {
        BSP_USART_USART_BASE->INTCLR |= (UART_FLAG_FE);
      }

      /* UART in mode Receiver -------------------------------------------------*/
      if (((isrflags & UART_INTSR_RI) != RESET) && ((sconits & UART_SCON_RIEN) != RESET))
      {
         BSP_USART_USART_BASE->INTCLR |= (UART_FLAG_RXNE);
         date[0]=(uint8_t)(BSP_USART_USART_BASE->SBUF & (uint8_t)0xFF);
      }

    } /* End if error occurs */

    /* UART in mode Transmitter ------------------------------------------------*/
    if(((isrflags & UART_INTSR_TI) != RESET) && ((sconits & UART_SCON_TIEN) != RESET))
    {
       BSP_USART_USART_BASE->INTCLR |= (UART_FLAG_TC);
       TcFlag=SET; 
    }
    
}